package com.ruoyi.etravel.service.impl;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.etravel.mapper.CommentMapper;
import com.ruoyi.etravel.pojo.dto.ScenicSpotDto;
import com.ruoyi.etravel.pojo.entity.Comment;
import com.ruoyi.etravel.service.ICommentService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.base.BaseService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.etravel.mapper.ScenicSpotMapper;
import com.ruoyi.etravel.pojo.entity.ScenicSpot;
import com.ruoyi.etravel.service.IScenicSpotService;

/**
 * 景点信息Service业务层处理
 *
 * @author caochong
 * @date 2022-11-11
 */
@Service
public class ScenicSpotServiceImpl extends ServiceImpl<ScenicSpotMapper,ScenicSpot> implements IScenicSpotService
{
    @Autowired
    private ScenicSpotMapper scenicSpotMapper;

    @Autowired
    private ICommentService commentService;


    /**
     * 根据景区id查询相关景区信息以及景区相关的评论信息
     * @param id
     * @return
     */
    @Override
    public ScenicSpot findScenicCommentById(Integer id) {
        ScenicSpot scenicSpot = lambdaQuery().eq(ScenicSpot::getScenicId, id).one();
        List<Comment> list = commentService
                .lambdaQuery()
                .eq(StringUtils.isNotNull(scenicSpot.getScenicId()), Comment::getScenicId, scenicSpot.getScenicId()).list();
        scenicSpot.getDate().put("CommentList", list);
        return scenicSpot;
    }

    /**
     * 根据景区信息分页查询相关的景点信息
     * @param pageNum
     * @param pageSize
     * @param scenicSpotDto
     * @return
     */
    @Override
    public AjaxResult pageScenicList(int pageNum, int pageSize, ScenicSpotDto scenicSpotDto) {
        //判断页码和每页的显示数量否大于零
        if (pageNum>0 && pageSize>0){
            Page<ScenicSpot> page = lambdaQuery()
                    .like(StringUtils.isNotEmpty(scenicSpotDto.getName()), ScenicSpot::getName, scenicSpotDto.getName())
                    .like(StringUtils.isNotEmpty(scenicSpotDto.getAddress()), ScenicSpot::getAddress, scenicSpotDto.getAddress())
                    .like(StringUtils.isNotEmpty(scenicSpotDto.getGrade()), ScenicSpot::getGrade, scenicSpotDto.getGrade())
                    .page(new Page<>(pageNum, pageSize));
            return AjaxResult.success(page);
        }else {
            return AjaxResult.error("页码数据不正确，请重新输入!");
        }
    }

    public <T> QueryWrapper<T> createSelWrapper(Class<T> type) {
        QueryWrapper<T> queryWrapper = Wrappers.query();
        queryWrapper.eq("del_flag", "0");
        return queryWrapper;
    }

    /**
     * 查询景点信息
     *
     * @param scenicId 景点信息主键
     * @return 景点信息
     */
    @Override
    public ScenicSpot selectScenicSpotByScenicId(Long scenicId)
    {
        return scenicSpotMapper.selectById(scenicId);
    }

    /**
     * 查询景点信息列表
     *
     * @param scenicSpot 景点信息
     * @return 景点信息
     */
    @Override
    public List<ScenicSpot> selectScenicSpotList(ScenicSpot scenicSpot)
    {
        QueryWrapper<ScenicSpot> queryWrapper = createSelWrapper(ScenicSpot.class);
        queryWrapper.lambda()
                .eq(StringUtils.isNotNull(scenicSpot.getScenicId()), ScenicSpot::getScenicId, scenicSpot.getScenicId())
                .like(StringUtils.isNotEmpty(scenicSpot.getName()), ScenicSpot::getName, scenicSpot.getName())
                .eq(StringUtils.isNotEmpty(scenicSpot.getGrade()), ScenicSpot::getGrade, scenicSpot.getGrade())
                .eq(StringUtils.isNotEmpty(scenicSpot.getLocation()), ScenicSpot::getLocation, scenicSpot.getLocation())
                .eq(StringUtils.isNotEmpty(scenicSpot.getAddress()), ScenicSpot::getAddress, scenicSpot.getAddress())
                .eq(StringUtils.isNotEmpty(scenicSpot.getNotice()), ScenicSpot::getNotice, scenicSpot.getNotice())
                .eq(StringUtils.isNotEmpty(scenicSpot.getTone()), ScenicSpot::getTone, scenicSpot.getTone())
                .eq(StringUtils.isNotEmpty(scenicSpot.getLal()), ScenicSpot::getLal, scenicSpot.getLal())
                .eq(StringUtils.isNotEmpty(scenicSpot.getDelFlag()), ScenicSpot::getDelFlag, scenicSpot.getDelFlag())
        ;
        return scenicSpotMapper.selectList(queryWrapper);
    }

    /**
     * 新增景点信息
     *
     * @param scenicSpot 景点信息
     * @return 结果
     */
    @Override
    public int insertScenicSpot(ScenicSpot scenicSpot)
    {
        scenicSpot.setCreateBy(SecurityUtils.getUsername());
        scenicSpot.setCreateTime(DateUtils.getNowDate());
        scenicSpot.setUpdateBy(SecurityUtils.getUsername());
        scenicSpot.setUpdateTime(DateUtils.getNowDate());
        int rows = scenicSpotMapper.insert(scenicSpot);
        return rows;
    }

    /**
     * 修改景点信息
     *
     * @param scenicSpot 景点信息
     * @return 结果
     */
    @Override
    public int updateScenicSpot(ScenicSpot scenicSpot)
    {
        scenicSpot.setUpdateBy(SecurityUtils.getUsername());
        scenicSpot.setUpdateTime(DateUtils.getNowDate());

        return scenicSpotMapper.updateById(scenicSpot);
    }

    /**
     * 批量删除景点信息
     *
     * @param scenicIds 需要删除的景点信息主键
     * @return 结果
     */
    @Override
    public int deleteScenicSpotByScenicIds(Long[] scenicIds)
    {
        return scenicSpotMapper.deleteBatchIds(Arrays.asList(scenicIds));
    }

    /**
     * 删除景点信息信息
     *
     * @param scenicId 景点信息主键
     * @return 结果
     */
    @Override
    public int deleteScenicSpotByScenicId(Long scenicId)
    {
        return scenicSpotMapper.deleteById(scenicId);
    }
}
